package com.dabai.addtofavorites.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.dabai.addtofavorites.R;
import com.dabai.addtofavorites.bean.FavData;
import com.dabai.addtofavorites.utils.MyFileUtils;
import com.dabai.dbutils.utils.DabaiUtils;
import com.dabai.dbutils.utils.StringUtils;

public class ShareAddMyActivity extends Activity {

    TextView te1, te2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_add);

        te1 = findViewById(R.id.textView1);
        te2 = findViewById(R.id.textView2);


        Intent intent = getIntent();
        try {

            if (intent == null)
                return;
            Bundle extras = intent.getExtras();

            if (extras == null)
                return;

            if ("text/plain".equals(intent.getType())) {//分享的内容类型，如果png图片：image/png

                String text = "" + extras.get(Intent.EXTRA_TEXT);

                if (StringUtils.isEmpty(text)) {
                    DabaiUtils.showToast(this, "异常:空内容!");
                    finish();
                    return;
                }

                releaseText(text);
                finish();

            }

        } catch (Exception e) {
            DabaiUtils.showToast(this, "异常:" + e.getMessage());
            Log.d("DBTAG", "onCreate: " + e.getMessage());
            DabaiUtils.backHome(this);
            finish();
        }


    }

    /**
     * 发布到本地仓库和列表
     *
     * @param text 内容
     */
    public void releaseText(String text) {

        if (text == null) {
            DabaiUtils.showToast(getApplicationContext(), "收藏失败!");
            return;
        }

        FavData favData = new FavData();
        favData.setContent(text);

        if (MyFileUtils.createFile(getApplicationContext(), favData)) {


            DabaiUtils.showToast(getApplicationContext(), "收藏成功!");


        } else {
            DabaiUtils.showToast(getApplicationContext(), "收藏失败!");
        }

    }

}